<template>
  <el-aside class="admin-aside" :width="asideWidth">
    <div class="toggle-button" @click="toggleCollapse">|||</div>
    <!-- 不使用菜单栏的路由模式 -->
    <el-menu :router="false" :default-active="$route.path" background-color="#545c64" text-color="#fff" active-text-color="#3f97f9" unique-opened :collapse="isCollapse" :collapse-transition="false">
      <el-menu-item index="/index" @click="saveMenuItemAndJump('/index')">
        <template slot="title">
          <i class="el-icon-milk-tea"></i>
          <span>前台首页</span>
        </template>
      </el-menu-item>
      <!-- 一级菜单 -->
      <el-submenu v-for="submenu in menuList" :index="submenu.name" :key="submenu.name">
        <template slot="title">
          <i :class="submenu.iconCls"></i>
          <span>{{ submenu.nameZh }}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index=item.path v-for="item in submenu.children" :key="item.name" @click="saveMenuItemAndJump(item.path)">
          <template slot="title">
            <i :class="item.iconCls"></i>
            <span>{{ item.nameZh }}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
  </el-aside>
</template>

<script>
export default {
  name: "AdminMenu",
  data() {
    return {
      // 菜单列表
      menuList: [],
      // 当前活跃的菜单项
      activePath: "",
      // 侧边栏宽度
      asideWidth: "200px",
      // 侧边栏是否隐藏
      isCollapse: false,
    };
  },
  created() {
    this.menuList = this.$store.state.menuList;
    this.activePath = this.$route.path;
  },
  methods: {
    // 点击切换侧边菜单列表的状态
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) return (this.asideWidth = "64px");
      this.asideWidth = "200px";
    },

    // 保存导航栏的状态并跳转
    saveMenuItemAndJump(activePath) {
      if (activePath === "/index") return this.$router.replace(activePath);
      this.activePath = activePath;
      this.$router.push(activePath);
    },
  },
};
</script>

<style lang="less" scoped>
.admin-aside {
  background-color: #333744;
  color: #ffffff;
  text-align: left;
}

.el-menu {
  width: 100%;
  border: none;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 45px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>